package com.ly.blog2Word.service.impl;

import com.ly.blog2Common.core.page.MybatisPageHelper;
import com.ly.blog2Common.core.page.PageRequest;
import com.ly.blog2Common.core.page.PageResult;
import com.ly.blog2Word.dao.WordLabelMapper;
import com.ly.blog2Word.dao.model.WordArticle;
import com.ly.blog2Word.dao.model.WordLabel;
import com.ly.blog2Word.service.WordLabelService;
import com.ly.blog2Word.utils.ArticleUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * @author: lyMaster
 * @time: 2020/7/17 19:07
 */
@Service
public class WordLabelImpl implements WordLabelService {

    @Autowired
    private WordLabelMapper wordLabelMapper;

    @Override
    public int save(WordLabel record) {
        if(record.getId() == null || record.getId() == 0) {
            return wordLabelMapper.insertSelective(record);
        }
        return wordLabelMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int delete(WordLabel record) {
        return wordLabelMapper.deleteByPrimaryKey(record.getId().intValue());
    }

    @Override
    public int delete(List<WordLabel> records) {
        for(WordLabel record:records) {
            delete(record);
        }
        return 1;
    }

    @Override
    public WordLabel findById(Long id) {
        return wordLabelMapper.selectByPrimaryKey(id.intValue());
    }

    @Override
    public PageResult findPage(PageRequest pageRequest) {
        return MybatisPageHelper.findPage(pageRequest, wordLabelMapper);
    }

    @Override
    public Object findPage() {
        return wordLabelMapper.findPage();
    }

    @Override
    public Object findLabelList(Integer id) {
        List<Map<String,String>> labelList= wordLabelMapper.findLabelList(id);

        for(Object o:labelList){
            if(o instanceof Map){
                Map<String,String> contentObj = (Map<String,String>) o;
                String content = contentObj.get("content");
                content = ArticleUtils.cleanHtmlAndLimitLength(content);
                contentObj.put("content",content);
            }
        }
        return labelList;

    }

}
